package net.huashitong.Indicators.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.Analysis;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.AnalysisService;
import net.huashitong.Indicators.service.CompletionClassificationService;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.Indicators.service.SolidInvestmentService;
import net.huashitong.Indicators.service.TertiaryIndustryService;
import net.huashitong.Indicators.utils.CodeUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
import net.huashitong.Indicators.utils.ThiTitleUtils;
import net.huashitong.scheduling.utils.IsUserUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
/**
 * 固投
 * @author lzr
 *
 */
@Controller
@RequestMapping("/app/solidInvestment/")
public class SolidInvestmentAppController extends JsonBaseController {

	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Autowired
	private CompletionClassificationService classificationService;
	@Autowired
	private SolidInvestmentService solidInvestmentService;
	@Autowired
	private AnalysisService analysisService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 固投
	 * @param filter
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping("/queryCountyRank")
	public Message queryCountyRank(@RequestBody IndicatorBreakdownFilter filter) throws ParseException{
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		IsUserUtils isuts=new IsUserUtils();
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		//计算全市各县区排名情况
			IndicatorBreakdownDatabean databean = solidInvestmentService.queryCountyRank(filter);
			if(ObjectUtils.isNotBlank(databean)){
				map.put("type", "1");
				map.put("dataInner", databean);
				map.put("unit", "亿元");
				map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
				map.put("descOne", date.substring(0, 4)+ThiTitleUtils.GXQZZL);
				map.put("descTwo", date.substring(0, 4)+ThiTitleUtils.GXQJDZ);
				map.put("title", "该指标全市排名一览");
				map.put("secTitle", "固定资产投资总额全市各县区排名情况");
				maps.add(map);
			}
			//当月同比增长情况
			//查找当月份上个月份的
			filter.setDate(date);
			filter.setCurrentMonth(cuDate);
			List<IndicatorBreakdownDatabean> databeans = solidInvestmentService.queryMonthDevelopment(filter);
			if(ObjectUtils.isNotBlank(databeans)){
				map = new HashMap<String, Object>();
				map.put("type", "2");
				map.put("descOne", ThiTitleUtils.DYTBZZL);
				map.put("descTwo", ThiTitleUtils.DYLJZZL);
				map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
				map.put("data", databeans);
				map.put("title", "该指标发展情况");
				map.put("secTitle", "固定资产投资总额累计运行情况");
				maps.add(map);
			}
			//各月绝对值及同比增长情况
			filter.setDate(date);
			filter.setCurrentMonth(cuDate);
			List<Map<String, Object>> list = solidInvestmentService.queryMonthAbsolute(filter);
			/*if(ObjectUtils.isNotBlank(list)){
				map = new HashMap<String, Object>();
				map.put("descOne", date.substring(0, 4)+ThiTitleUtils.GYTBZZL);
				map.put("descTwo", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.GYJDZ);
				map.put("descThi", date.substring(0, 4)+ThiTitleUtils.GYJDZ);
				map.put("type", "3");
				map.put("unit", "亿元");
				map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
				map.put("data", list);
				map.put("title", "该指标各月同比增长情况");
				map.put("secTitle", "固定资产投资总额各月"+ThiTitleUtils.JDZ+"和同比增长情况");
				maps.add(map);
			}*/
			//累计比增同比增长情况
			filter.setDate(date);
			filter.setCurrentMonth(cuDate);
			list = solidInvestmentService.queryLJAbsolute(filter);
			if(ObjectUtils.isNotBlank(list)){
				map = new HashMap<String, Object>();
				map.put("type", "4");
				map.put("descOne", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.GYTBZZL);
				map.put("descTwo", date.substring(0, 4)+ThiTitleUtils.GYTBZZL);
				map.put("descThi", DateForIndUtils.getNextYearDate(date.substring(0, 4),-1)+ThiTitleUtils.GYLJJDZ);
				map.put("descFou", date.substring(0, 4)+ThiTitleUtils.GYLJJDZ);
				map.put("data", list);
				map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
				map.put("unit", "亿元");
				map.put("title", "该指标累计同比增长情况");
				map.put("secTitle", "固定资产投资总额累计"+ThiTitleUtils.JDZ+"与同比增长情况");
				maps.add(map);
			}
			
			//50万元以上完成情况
			filter.setDate(date);
			filter.setCurrentMonth(cuDate);
			List<CompletionClassificationDatabean> classificationDatabean2s = solidInvestmentService.queryByXZValue(filter);
			if(ObjectUtils.isNotBlank(classificationDatabean2s)){
				map = new HashMap<String, Object>();
				map.put("type", "5");
				map.put("unit", "万元");
				map.put("descOne", ThiTitleUtils.WCBL);
				map.put("descTwo", ThiTitleUtils.JSDW);
				map.put("descThi", ThiTitleUtils.WCS);
				map.put("descFou", ThiTitleUtils.JHS);
				map.put("category", CodeUtils.category_XZ);
				map.put("data", classificationDatabean2s);
				map.put("title", "该指标各乡镇完成情况");
				map.put("secTitle", "分乡镇50万元以上固定资产投资总额完成情况");
				maps.add(map);

			}
			//部门累计完成情况
			filter.setDate(date);
			filter.setCurrentMonth(cuDate);
			classificationDatabean2s = solidInvestmentService.queryByValue(filter);
			if(ObjectUtils.isNotBlank(classificationDatabean2s)){
				map = new HashMap<String, Object>();
				map.put("type", "5");
				map.put("unit", "亿元");
				map.put("descOne", ThiTitleUtils.WCBL);
				map.put("descTwo", ThiTitleUtils.JSDW);
				map.put("descThi", ThiTitleUtils.WCS);
				map.put("descFou", ThiTitleUtils.JHS);
				map.put("category", CodeUtils.category_BM);
				map.put("data", classificationDatabean2s);
				map.put("title", "该指标各部门完成情况");
				map.put("secTitle", "固定资产投资总额前十大部门累计完成情况");
				maps.add(map);

			}
						
			//按行业分类占比情况
			filter.setDate(date);
			filter.setCurrentMonth(cuDate);
			List<Map<String, Object>> list2 = solidInvestmentService.queryByHYValue(filter);
			if(ObjectUtils.isNotBlank(list2)){
				map = new HashMap<String, Object>();
				map.put("type", "6");
				map.put("data", list2);
				map.put("descOne", "房地产投资"+ThiTitleUtils.ZB);
				map.put("descTwo", "项目投资"+ThiTitleUtils.ZB);
				map.put("descThi", "项目投资"+ThiTitleUtils.JDZ);
				map.put("descFou", "房地产开发投资"+ThiTitleUtils.JDZ);
				map.put("category", CodeUtils.category_HY);
				map.put("title", "该指标各行业分类占比情况");
				map.put("unit", "亿元");
				map.put("secTitle", "固定资产投资总额按行业分类占比情况");
				maps.add(map);
			}
			//单位完成情况
			filter.setDate(date);
			filter.setCurrentMonth(cuDate);
			list2 = solidInvestmentService.queryByDWValue(filter);
			if(isuts.getIsUser(filter.getUserId())){
			if(ObjectUtils.isNotBlank(list2)){
				map = new HashMap<String, Object>();
				map.put("type", "7");
				map.put("data", list2);
				map.put("descOne", ThiTitleUtils.JSDW);
				map.put("descSec", ThiTitleUtils.WCBL);
				map.put("descThi", ThiTitleUtils.WWCBL);
				map.put("descFou", ThiTitleUtils.WCS);
				map.put("descFiv", ThiTitleUtils.JHS);
				map.put("category", CodeUtils.category_QY);
				map.put("title", "该指标各单位完成情况");
				map.put("unit", "万元");
				map.put("secTitle", "固定资产投资总额各单位完成情况");
				maps.add(map);
			}
			}
			filter.setDate(date);
			filter.setCurrentMonth(cuDate);
			filter.setBm("项目");
			List<CompletionClassification> classifications = classificationService.queryByValue(filter);
			if(ObjectUtils.isNotBlank(classifications)){
				/*int size = classifications.size();
				size = size/20;*/
				if(isuts.getIsUser(filter.getUserId())){
				for (int i = 0; i <1; i++) {
					filter.setDate(date);
					filter.setPage(i);
					//部门累计完成情况
					filter.setDate(date);
					filter.setCurrentMonth(cuDate);
					classificationDatabean2s = solidInvestmentService.queryXMValue(filter);
					if(ObjectUtils.isNotBlank(classificationDatabean2s)){
						
						map = new HashMap<String, Object>();
						map.put("type", "5");
						map.put("unit", "万元");
						map.put("descOne", ThiTitleUtils.WCBL);
						map.put("descTwo", ThiTitleUtils.JSDW);
						map.put("descThi", ThiTitleUtils.JHS);
						map.put("descFou", ThiTitleUtils.WCS);
						map.put("category", CodeUtils.category_XM);
						map.put("data", classificationDatabean2s.subList(0, 19));
						map.put("title", "该指标各项目完成情况");
						map.put("secTitle", "固定资产投资总额各项目完成情况");
						maps.add(map);

					}
					
					
				}
			  }
			}
			
			
			map = new HashMap<String, Object>();
			filter.setCurrentMonth(cuDate);
			Analysis analysis = analysisService.getByIndIdAndDate(filter);
			if(ObjectUtils.isNotBlank(analysis)){
			    	map.put("secTitle", analysis.getText());
			 		map.put("type", "0");
			 		map.put("title", "该指标分析");
			 		maps.add(map);
		    }
			
			map = new HashMap<String, Object>();
			filter.setCurrentMonth(cuDate);
		    TertiaryIndustry industry = industryService.queryByfilter(filter);
		    if(ObjectUtils.isNotBlank(industry)){
		    	map.put("leadership", industry.getLeadership());
				map.put("unitsNumber", industry.getUnitsNumber());
		    }
		    map.put("lists", maps);
		    map.put("monthOrQuarter", CodeUtils.monthOrQuarter_mon);
			map.put("title", "固定资产投资总额运行情况");
		return coverMessage("200", "获取成功", map);
	}
//	/**
//	 * 处理可比累计价格
//	 * @param breakdowns
//	 * @param breakdownDatabeans
//	 * @return
//	 */
//	public List<IndicatorBreakdownDatabean> detailcumulativeData(List<IndicatorBreakdown> breakdowns,List<IndicatorBreakdownDatabean> breakdownDatabeans){
//		for(int i = 0 ;i<breakdowns.size();i++){
//			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
//			IndicatorBreakdown breakdown = breakdowns.get(i);
//			databean.setCumulative(Double.toString(Math.abs(Double.parseDouble(breakdown.getCumulative()))));
//			databean.setRatio(breakdown.getRatio());
//			databean = dataConversion(databean, breakdown);
//			breakdownDatabeans.add(databean);
//		}
//		return breakdownDatabeans;
//	}
//	/**
//	 * 数据转换
//	 * @param databean
//	 * @param breakdown
//	 * @return
//	 */
//	public IndicatorBreakdownDatabean dataConversion(IndicatorBreakdownDatabean databean,IndicatorBreakdown breakdown){
//		databean.setDate(breakdown.getDate());
//		databean.setCurrentMonth(breakdown.getCurrentMonth());
//		return databean;
//	}
//	/**
//	 * 数据处理当月可比价格
//	 * @param breakdowns
//	 * @param breakdownDatabeans
//	 * @return
//	 */
//	public List<IndicatorBreakdownDatabean> detailData(List<IndicatorBreakdown> breakdowns,List<IndicatorBreakdownDatabean> breakdownDatabeans){
//		for(int i = 0 ;i<breakdowns.size();i++){
//			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
//			IndicatorBreakdown breakdown = breakdowns.get(i);
//			if(i == breakdowns.size()-1){
//				Double double1 = Double.parseDouble(breakdown.getCumulative());
//				databean.setSeasonsComparison(Double.toString(double1/2).substring(0,Double.toString(double1/2).length()-4));
//			}else {
//				IndicatorBreakdown breakdown2 = breakdowns.get(i+1);
//				Double double1 = Double.parseDouble(breakdown.getCumulative());
//				Double double2 = Double.parseDouble(breakdown2.getCumulative());
//				databean.setSeasonsComparison(Double.toString(double1-double2).substring(0, Double.toString(double1-double2).length()-4));
//			}
//			databean = dataConversion(databean, breakdown);
//			breakdownDatabeans.add(databean);
//		}
//		return breakdownDatabeans;
//	}
	/**
	 * 固定资产投资总额各县市排名三级页面
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryCountyRankThi")
	public Message queryCountyRankThi(@RequestBody IndicatorBreakdownFilter filter){
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		Map<String, Object> maps = solidInvestmentService.queryCountyRankThi(filter);
		return coverMessage("200", "数据获取成功", maps);
	}
	/**
	 * 各月绝对值及同比增长情况三级数据
	 * @param filter
	 * @return
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping("/queryMonthAbsoluteThi")
	public Message queryMonthAbsoluteThi(@RequestBody IndicatorBreakdownFilter filter) throws ParseException{
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		Map<String, Object> maps = solidInvestmentService.queryMonthAbsoluteThi(filter);
		return coverMessage("200", "数据获取成功", maps);
	}
	/**
	 * 累计比增同比增长情况三级数据
	 * @throws ParseException 
	 */
	@ResponseBody
	@RequestMapping("/queryMonthCountAbsoluteThi")
	public Message queryMonthCountAbsoluteThi(@RequestBody IndicatorBreakdownFilter filter) throws ParseException{
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		Map<String, Object> maps = solidInvestmentService.queryMonthCountAbsoluteThi(filter);



		return coverMessage("200", "数据获取成功", maps);
	}
	/**
	 * 乡镇三级数据
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryByXZValueThi")
	public Message queryByXZValueThi(@RequestBody IndicatorBreakdownFilter filter){
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		Map<String, Object> maps = solidInvestmentService.queryByXZValueThi(filter);
		return coverMessage("200", "数据获取成功", maps);
	}
	/**
	 * 固定资产投资当月同比增长情况
	 * @param filter
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping("/queryMonthDevelopmentSituationThi")
	public Message queryMonthDevelopmentSituationThi(@RequestBody IndicatorBreakdownFilter filter) throws ParseException{
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		Map<String, Object> maps = solidInvestmentService.queryMonthDevelopmentSituationThi(filter);
		return coverMessage("200", "数据获取成功", maps);
	}
	/**
	 * 部门
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryByBMValueThi")
	public Message queryByBMValueThi(@RequestBody IndicatorBreakdownFilter filter){
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		Map<String, Object> maps = solidInvestmentService.queryByBMValueThi(filter);
		return coverMessage("200", "数据获取成功", maps);
	}
	/**
	 * 企业
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryByDWValueThi")
	public Message queryByDWValueThi(@RequestBody IndicatorBreakdownFilter filter){
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		Map<String, Object> maps = solidInvestmentService.queryByDWValueThi(filter);
		return coverMessage("200", "数据获取成功", maps);
	}
	/**
	 * 三级代码
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryLastThiValue")
	public Message queryLastThiValue(@RequestBody IndicatorBreakdownFilter filter){
		String date = DateForIndUtils.getMonth(filter.getDate());
		String cuDate = DateForIndUtils.getCMonth(filter.getDate());
		filter.setDate(date);
		filter.setCurrentMonth(cuDate);
		Map<String, Object> maps = solidInvestmentService.queryLastThiValue(filter);
		return coverMessage("200", "数据获取成功", maps);
	}
}
